package com.lemonPiggy.UDForm.orm.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.lemonPiggy.UDForm.domain.UDFormField;
import com.lemonPiggy.UDForm.domain.UDFormWebPage;
import com.lemonPiggy.UDForm.mapper.UDFormApplicationMapper;
import com.lemonPiggy.UDForm.mapper.UDFormFieldMapper;
import com.lemonPiggy.UDForm.mapper.UDFormWebPageMapper;
import com.ruoyi.common.core.utils.IdUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.lemonPiggy.UDForm.service.IUDFormFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lemonPiggy.UDForm.service.IUDFormWebPageService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 页面自定义Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-02
 */
@Service
public class UDFormWebPageServiceImpl implements IUDFormWebPageService
{
    @Autowired(required=false)
    private UDFormWebPageMapper UDFormWebPageMapper;
    @Autowired(required=false)
    private UDFormFieldMapper UDFormFieldMapper;
    @Autowired(required=false)
    private IUDFormFieldService UDFormFieldService;
    @Autowired(required=false)
    private UDFormApplicationMapper UDFormApplicationMapper;


    /**
     * 查询页面自定义
     * 
     * @param id 页面自定义ID
     * @return 页面自定义
     */
    //查询WebPage页面的id
    @Override
    public UDFormWebPage selectUDFormWebPageById(String id)
    {
        UDFormField UDFormField = new UDFormField();
        //把查询出来WebPage的id给filed页面
        UDFormField.setWebPageId(id);
        //在filed页面拿到的WebPage的信息
        List<UDFormField> list = UDFormFieldMapper.selectUDFormFieldList(UDFormField);
        //把WebPage页面的信息查询出来
        UDFormWebPage UDFormWebPage = UDFormWebPageMapper.selectUDFormWebPageById(id);
        //将信息set进filed的集合当中去。
        UDFormWebPage.setFieldList(list);
        return UDFormWebPage;
    }


    /**
     * 查询页面自定义列表
     * 
     * @param UDFormWebPage 页面自定义
     * @return 页面自定义
     */
    @Override
    public List<UDFormWebPage> selectUDFormWebPageList(UDFormWebPage UDFormWebPage)
    {
        return UDFormWebPageMapper.selectUDFormWebPageList(UDFormWebPage);
    }

    /**
     * 新增页面自定义
     * 
     * @param UDFormWebPage 页面自定义
     * @return 结果
     */
    @Transactional
    @Override
    public int insertUDFormWebPage(UDFormWebPage UDFormWebPage)
    {
        String uuid = IdUtils.simpleUUID();
        UDFormWebPage.setId(uuid);
        UDFormWebPage.setCreatedBy(SecurityUtils.getUsername());
        UDFormWebPage.setUpdatedBy(SecurityUtils.getUsername());
        UDFormWebPage.setCreatedTime(new Date());
        UDFormWebPage.setUpdatedTime(new Date());
        int n= UDFormWebPageMapper.insertUDFormWebPage(UDFormWebPage);
        if(n>0){
            List<UDFormField> list= UDFormWebPage.getFieldList();
            int i=0;
            if (list==null){
                return 1;
            }
            for(UDFormField ff:list){
                ff.setWebPageId(uuid);
                i=i+UDFormFieldService.insertUDFormField(ff);
            }
            if(i==list.size()){
                return 1;
            }
        }

        return 0;
    }

    /**
     * 修改页面自定义
     * 
     * @param UDFormWebPage 页面自定义
     * @return 结果
     */
    @Transactional
    @Override
    public int updateUDFormWebPage(UDFormWebPage UDFormWebPage) {
        UDFormField UDFormField = new UDFormField();
        UDFormField.setWebPageId(UDFormWebPage.getId());
        List<UDFormField> list = UDFormFieldMapper.selectUDFormFieldList(UDFormField);
        int i = 0;
        for (UDFormField diyField : list) {
            i=i+ UDFormFieldMapper.deleteUDFormFieldById(diyField.getId());
        }
        if(i==list.size()){
            List<UDFormField> list1 = UDFormWebPage.getFieldList();
            i = 0;
            for (UDFormField ff : list1) {
                ff.setWebPageId(UDFormWebPage.getId());
                i = i + UDFormFieldService.insertUDFormField(ff);
            }
            if (i == list1.size()) {
                UDFormWebPage.setUpdatedBy(SecurityUtils.getUsername());
                UDFormWebPage.setUpdatedTime(new Date());
                return UDFormWebPageMapper.updateUDFormWebPage(UDFormWebPage);
            }
        }
        return 0;
    }

    /**
     * 批量删除页面自定义
     *
     * @param ids 需要删除的页面自定义ID
     * @return 结果
     */
    @Override
    public int deleteUDFormWebPageByIds(String[] ids)
    {
        for (String id : ids) {
         UDFormFieldMapper.deletePageId(id);
        }
        return UDFormWebPageMapper.deleteUDFormWebPageByIds(ids);
    }

    /**
     * 删除页面自定义信息
     *
     * @param id 页面自定义ID
     * @return 结果
     */
    @Override
    public int deleteUDFormWebPageById(String id)
    {
        return UDFormWebPageMapper.deleteUDFormWebPageById(id);
    }

    @Override
    public List<Map<String, Object>> optionselect(String appId) {
        return UDFormWebPageMapper.optionselect(appId);
    }

    @Override
    public List<UDFormWebPage> selectApplication(String id) {
        return UDFormWebPageMapper.selectApplication(id);
    }

    @Override
    public int updatePage(UDFormWebPage UDFormWebPage) {
        return UDFormWebPageMapper.updatePage(UDFormWebPage);
    }


}
